﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace BZ.Common
{
    public class StringUtility
    {
        #region 字符串相关处理

        /// <summary>
        /// 根据长度截取前半部分摘要内容，同时依照省略符结尾。
        /// </summary>
        /// <param name="_content">内容</param>
        /// <param name="_strLength">显示长度</param>
        /// <param name="_ellipsis">省略符</param>
        /// <returns></returns>
        public static string GetDigestContent(object _content, int _strLength, string _ellipsis)
        {
            if (_content != null)
            {
                string result = _content.ToString();
                if (result.Length > _strLength)
                {
                    result = result.Substring(0, _strLength) + _ellipsis;
                }
                return result;
            }
            else
            {
                return string.Empty;
            }
        }


        /// <summary>
        /// 清除文本前面的序号
        /// </summary>
        /// <param name="_text">文本</param>
        /// <param name="_nosplitchar">分割符</param>
        /// <returns></returns>
        public static string ClearTextNo(string _text, char _nosplitchar)
        {
            if (!string.IsNullOrEmpty(_text))
            {
                int myIndex = _text.IndexOf(_nosplitchar);
                if (myIndex > 0 && _text.Length > (myIndex + 1))
                {
                    return _text.Substring(myIndex + 1);
                }
                else
                {
                    return _text;
                }
            }
            return string.Empty;
        }

        /// <summary>
        /// 转换字符串中的转义字符，转换成HTML显示的内容
        /// 现在支持：
        ///     \n  转  <br/>
        /// </summary>
        /// <param name="_source"></param>
        /// <param name="_isHtmlEncode">是否进行Html编码</param>
        /// <returns></returns>
        public static string StringToHtml(string _source, bool _isHtmlEncode = false)
        {
            if (!string.IsNullOrEmpty(_source))
            {
                if (_isHtmlEncode)
                {
                    _source = HttpUtility.HtmlEncode(_source);
                }
                StringBuilder myHtmlString = new StringBuilder(_source);
                myHtmlString = myHtmlString.Replace("\n", "<br/>");
                return myHtmlString.ToString();
            }
            else
            {
                return string.Empty;
            }
        }




        /// <summary>
        /// 用Md5方法来比较两个大字符串
        /// </summary>
        /// <param name="_stringA"></param>
        /// <param name="_stringB"></param>
        /// <returns></returns>
        public static bool IsEqual(string _stringA, string _stringB)
        {
            if (string.Compare(CommonUtility.GetMD5Str(_stringA, false).Trim(), CommonUtility.GetMD5Str(_stringB, false).Trim(), true) == 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 获取带有前后缀的字符串
        /// </summary>
        /// <param name="_source">词列表</param>
        /// <param name="_startStr">词前缀</param>
        /// <param name="_endStr">词后缀</param>
        /// <param name="_splitFlag">分割</param>
        /// <param name="_isRemove">是否删除分割</param>
        /// <returns></returns>
        public static string GetStringByList(IEnumerable<string> _source, string _startStr, string _endStr, string _splitFlag, bool _isRemove = true)
        {
            if (_source != null)
            {
                if (_source.Count() > 0)
                {
                    StringBuilder mykeys = new StringBuilder(100);
                    foreach (string onekey in _source)
                    {
                        mykeys.Append(_startStr);
                        mykeys.Append(onekey);
                        mykeys.Append(_endStr);
                        mykeys.Append(_splitFlag);
                    }
                    if (_isRemove == true)
                    {
                        mykeys.Remove(mykeys.Length - _splitFlag.ToString().Length, _splitFlag.ToString().Length);
                    }
                    return mykeys.ToString();
                }
            }
            return string.Empty;
        }

        /// <summary>
        /// 根据列表返回指定间隔符的字符串
        /// </summary>
        /// <param name="_source">列表</param>
        /// <param name="_splitFlag">间隔符</param>
        /// <param name="_isRemove">是否移除尾部的间隔符</param>
        /// <returns></returns>
        public static string GetStringByList(IEnumerable<string> _source, string _splitFlag, bool _isRemove = true)
        {
            return GetStringByList(_source, string.Empty, string.Empty, _splitFlag, _isRemove);
        }

        /// <summary>
        /// 根据列表返回指定间隔符的字符串，默认去掉尾部
        /// </summary>
        /// <param name="_source">列表</param>
        /// <param name="_splitFlag">间隔符</param>
        /// <returns></returns>
        public static string GetStringByList(IEnumerable<string> _source, char _splitFlag)
        {
            return GetStringByList(_source, _splitFlag.ToString(), true);
        }

        /// <summary>
        /// 根据列表返回指定间隔符的字符串，默认去掉尾部
        /// </summary>
        /// <param name="_source">列表</param>
        /// <param name="_splitFlag">间隔符</param>
        /// <returns></returns>
        public static string GetStringByList(IEnumerable<string> _source, string _splitFlag)
        {
            return GetStringByList(_source, _splitFlag, true);
        }

        /// <summary>
        /// 输入时使用的通用分割符组
        /// </summary>
        private static char[] _splitCharArray = { ' ', '　', ',', ';', '\'', '"', '，', '.', '。', '；', '‘', '’', '“', '”', '、' };

        /// <summary>
        /// 依照默认分割符分割信息
        /// </summary>
        /// <param name="_source">源串</param>
        /// <returns></returns>
        public static List<string> GetStringList(string _source)
        {
            return GetStringList(_source, null);
        }

        /// <summary>
        /// 分割信息为列表
        /// </summary>
        /// <param name="_source">源串</param>
        /// <param name="_splitcChars">分隔符，可空</param>
        /// <returns></returns>
        public static List<string> GetStringList(string _source, params char[] _splitcChars)
        {
            if (_splitcChars == null || _splitcChars.Count() == 0)
            {
                _splitcChars = _splitCharArray;
            }
            List<string> myStringList = new List<string>();
            if (!string.IsNullOrEmpty(_source))
            {
                foreach (string cellstr in _source.Split(_splitcChars))
                {
                    if (!string.IsNullOrEmpty(cellstr))
                    {
                        myStringList.Add(cellstr);
                    }
                }
            }
            return myStringList;
        }

        /// <summary>
        /// 获取字符串
        /// </summary>
        /// <param name="_source">来源</param>
        /// <returns></returns>
        public static string GetString(object _source)
        {
            if (_source != null && !string.IsNullOrEmpty(_source.ToString()))
            {
                return _source.ToString();
            }
            else
            {
                return "无";
            }
        }




        /// <summary>
        /// 带默认值获取信息，如果需要获取的信息为空 则返回默认值
        /// </summary>
        /// <param name="_source">来源值</param>
        /// <param name="_defautStr">默认值</param>
        /// <returns></returns>
        public static string GetStringWithDefault(object _source, object _defautStr)
        {
            if (_source != null)
            {
                if (!string.IsNullOrEmpty(_source.ToString().Trim()))
                {
                    return _source.ToString();
                }
            }
            return _defautStr.ToString();
        }


        /// <summary>
        /// 字符串截取补字符函,右侧补充
        /// </summary>
        /// <param name="_source">要处理的字符串</param>
        /// <param name="_length">长度</param>
        /// <param name="_padChar">补充的字符</param>
        /// <returns>处理后字符</returns>
        public static string splitStringLen(string _source, int _length, char _padChar)
        {
            if (string.IsNullOrEmpty(_source))
            {
                return "";
            }
            if (_source.Length >= _length)
            {
                return _source.Substring(0, _length);
            }
            return _source.PadRight(_length, _padChar);
        }

        /// <summary>
        /// 数组中是否含有指定字符串
        /// </summary>
        /// <param name="_source"></param>
        /// <param name="_ignoreCase"></param>
        /// <param name="_strArray"></param>
        /// <returns></returns>
        public static bool IsHaveMyString(string _source, bool _ignoreCase, params string[] _strArray)
        {
            bool result = false;
            if (!string.IsNullOrEmpty(_source) && _strArray != null && _strArray.Count() > 0)
            {
                foreach (string stringinlist in _strArray)
                {
                    result = string.Compare(_source.Trim(), stringinlist.Trim(), _ignoreCase) == 0 ? true : false;
                    if (result == true)
                    {
                        return true;
                    }
                }
            }
            return result;
        }


        #endregion
    }
}
